<script type="text/javascript">
var msgBackValidation="<fmt:message key="back_validation"/>";
var msgDenyValidation="<fmt:message key="deny_validation"/>";
var msgBackValidation2="<fmt:message key="back_validation2"/>";
var msgBackValidation3="<fmt:message key="back_validation3"/>";
var bondBackValidation = "<fmt:message key="bond_confirm_validation"/>";

function checkBack(type, confirmMsg){
    if (type=="sca"){
        if($("#singleAuditComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation);
            return false;
        }
    } else if(type=="qualification"){
        if($("#singleAuditComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation);
            return false;
        }
    } else if(type=="insurance"){
        if($("#singleAuditComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation2);
            return false;
        }
    } else if(type=="bank"){
        if($("#singleAuditComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation);
            return false;
        }
    } else if(type=="bank_deny"){
        if($("#singleDealerComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgDenyValidation);
            return false;
        }
    } else if(type=="ca_dealer"){
        if($("#singleDealerComment").val()!=""){
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation3);
            return false;
        }
    } else if(type=="ca_bank"){
        if($("#singleAuditComment").val()!="") {
            return confirm(confirmMsg);
        } else {
            alert(msgBackValidation);
            return false;
        }
    } else if(type=="bond"){
        if($("#singleAuditComment").val()!="") {
            return confirm(confirmMsg);
        } else {
            alert(bondBackValidation);
            return false;
        }
    }
}</script>

<c:if test="${loanRequest.dealerUser.username==pageContext.request.remoteUser or loanRequest.auditUser.username==pageContext.request.remoteUser}">
    <c:set var="actionEnable" scope="page" value="true" />
</c:if>

<c:if test="${(loanRequest.phase=='qualification_audit' or loanRequest.phase=='insurance_loan_audit' or loanRequest.phase=='insurance_confirm_policy')
    and loanRequest.dealer.masterProvider.bondUser.username==pageContext.request.remoteUser}">
    <c:set var="actionEnable" scope="page" value="true" />
</c:if>

<div class="box box-primary box-solid">
    <div class="box-header">
        <h3 class="box-title"><fmt:message key="loanRequestDetail.actionTitle"/></h3>
    </div>
    <!-- /.box-header -->
    <div class="box-body">
        <security:authorize ifAnyGranted="ROLE_CC">
            <c:if test="${actionEnable and loanRequest.status == 'request_audit'}">
                <div class="form-group">
                    <label class="control-label"><fmt:message key="loanRequest.pbocComment"/></label>
                    <form:textarea path="pbocComment" id="pbocComment" rows="2" cssClass="form-control" />
                </div>
                <a class="btn btn-default margin" target="blank" href="/personList?reqId=${loanRequest.id}"><fmt:message
                        key="loanRequestDetail.action.exportPersonList"/></a>
            </c:if>
        </security:authorize>

        <!-- dealer comments -->
        <c:if test="${viewInfo.dealerCommentVisible and actionEnable}">
            <div class="form-group">
                <label class="control-label"><fmt:message key="loanRequest.dealerComment"/>
                    <c:if test="${phaseInfo.dealerCommentTitle!='' and phaseInfo!=null}"><small style="color:blue;"><fmt:message key="${phaseInfo.dealerCommentTitle}"/></small></c:if>
                </label>
                <form:textarea path="dealerComment" id="dealerComment" rows="3" cssClass="form-control"
                               readonly="true"/>
                <c:if test="${phaseInfo.needDealerComment}">
                    <form:textarea path="singleDealerComment" id="singleDealerComment" rows="2"
                                   cssClass="form-control"/>
                </c:if>
            </div>
        </c:if>

        <!-- audit comments -->
        <c:if test="${viewInfo.auditCommentVisible and actionEnable}">
            <div class="form-group"/>
                <label class="control-label"><fmt:message key="loanRequest.auditComment"/>
                    <c:if test="${phaseInfo.auditCommentTitle!='' and phaseInfo!=null}"><small style="color:blue;"><fmt:message key="${phaseInfo.auditCommentTitle}"/></small></c:if>
                </label>
                <form:textarea path="auditComment" id="auditComment" rows="3" cssClass="form-control" readonly="true"/>
                <c:if test="${phaseInfo.needAuditComment}">
                    <form:textarea path="singleAuditComment" id="singleAuditComment" rows="2" cssClass="form-control"/>
                </c:if>
            </div>

        </c:if>

        <!-- CA/SCA audit code -->
        <security:authorize ifNotGranted="ROLE_DEALER_USER,ROLE_CC,ROLE_Insurance,ROLE_Bank_USER,ROLE_Bond_USER">
            <c:if test="${!(loanRequest.phase=='draft' || loanRequest.phase=='dealer_submit' || loanRequest.phase=='bank_credit_audit') and param.mode!='edit'}">
                <label class="margin"><form:radiobutton path="auditCode" value="approve" cssClass="insure" disabled="${!editCondition}"/><fmt:message key="loanRequest.approve"/></label>
                <label class="margin"><form:radiobutton path="auditCode" value="changeApprove" cssClass="insure" disabled="${!editCondition}"/><fmt:message key="loanRequest.changeApprove"/></label>
                <label class="margin"><form:radiobutton path="auditCode" value="deny" cssClass="insure" disabled="${!editCondition}"/><fmt:message key="loanRequest.deny"/></label>
                <label class="margin"><form:radiobutton path="auditCode" value="cancel" cssClass="insure" disabled="${!editCondition}"/><fmt:message key="loanRequest.cancel"/></label>
                <br/>
            </c:if>
        </security:authorize>

        <!-- loan date and bank contract no -->
        <security:authorize ifAnyGranted="ROLE_Insurance,ROLE_Bank_USER">
            <c:if test="${loanRequest.phase == 'insurance_confirm_policy' or loanRequest.phase == 'insurance_upload_policy' or loanRequest.phase == 'bank_loan_audit'
                    or loanRequest.phase == 'bank_pending_loan' or loanRequest.phase == 'loan_approved'}">
                <div class="form-group">
                    </br>
                    <label class="control-label"><fmt:message key="loanRequest.loanDate"/></label>
                    <form:input path="loanDate" id="loanDate" cssClass="form-control" disabled="${loanRequest.phase != 'bank_loan_audit'}"/>
                </div>
                <div class="form-group">
                    </br>
                    <label class="control-label"><fmt:message key="loanRequest.bankContractNo"/></label>
                    <form:input path="bankContractNo" id="bankContractNo" cssClass="form-control" disabled="${loanRequest.phase != 'bank_loan_audit'}"/>
                </div>
                <div class="form-group">
                    </br>
                    <label class="control-label"><fmt:message key="loanRequest.creditCardNo"/></label>
                    <form:input path="creditCardNo" id="creditCardNo" cssClass="form-control" disabled="${loanRequest.phase != 'bank_loan_audit'}"/>
                </div>
                <br/>
            </c:if>
            <c:if test="${loanRequest.phase == 'bank_pending_loan'}">
                <div class="form-group">
                    <label class="control-label"><fmt:message key="loanRequest.coverNumber"/></label>
                    <form:input path="coverNumber" id="coverNumber" cssClass="form-control" disabled="true"/>
                </div>
            </c:if>
        </security:authorize>

        <!-- cover note number -->
        <security:authorize ifAnyGranted="ROLE_Insurance">
            <c:if test="${loanRequest.phase == 'insurance_upload_policy' and actionEnable}">
                <div class="form-group">
                    <label class="control-label"><fmt:message key="loanRequest.coverNumber"/></label>
                    <form:input path="coverNumber" id="coverNumber" cssClass="form-control" disabled="${!actionEnable}"/>
                </div>
            </c:if>
            <br/>
        </security:authorize>
    <security:authorize ifAnyGranted="ROLE_CA">
        <c:if test="${actionEnable}">
            <button type="submit" name="caSaveCredit" class="btn btn-primary"><fmt:message key="button.save"/> </button>
        </c:if>
    </security:authorize>
        <!-- actions from xml -->
        <c:if test="${actionEnable}">
        <c:if test="${phaseInfo.canSave == true or copy== 'true'}">
            <button type="submit" class="btn btn-primary margin" id="save" name="save">
                <i class="fa fa-save"></i> <fmt:message key="button.save"/></button>
        </c:if>

        <c:forEach var="action" items="${phaseInfo.actions}">
            <c:if test="${action.sendforward == false or loanRequest.sendback == true}">
                <c:if test="${action.checkBackType!=''}">
                <button type="submit" class="btn btn-${action.buttonStyle} margin" id="${action.name}" name="${action.name}"
                        onclick="return checkBack('${action.checkBackType}','<fmt:message key="${action.confirmMsg}"/>');">
                    <fmt:message key="${action.label}"/>
                </button>
                </c:if>
                <c:if test="${action.checkBackType==''}">
                    <button type="submit" class="btn btn-${action.buttonStyle} margin" id="${action.name}" name="${action.name}"
                            onclick="return confirm('<fmt:message key="${action.confirmMsg}"/>');">
                    <fmt:message key="${action.label}"/>
                    </button>
                </c:if>
            </c:if>
        </c:forEach>
        </c:if>

        <!-- CA export -->
        <%--<security:authorize ifAnyGranted="ROLE_CA">--%>
            <%--<c:if test="${actionEnable and loanRequest.phase=='ca_audit' and param.mode != 'edit'}">--%>
                <%--<a href="/export/businessForm?reqId=${loanRequest.id}" class="btn btn-warning margin" id="exportBusinessForm" >--%>
                    <%--<fmt:message key="ACTION_exportBusinessForm"/></a>--%>
                <%--<a href="/export/insuranceForm?reqId=${loanRequest.id}" class="btn btn-warning margin" id="exportInsuranceForm" >--%>
                    <%--<fmt:message key="ACTION_exportInsuranceForm"/></a>--%>
                <%--<a href="/export/auditResult?reqId=${loanRequest.id}" class="btn btn-warning margin" id="exportAuditResult" >--%>
                    <%--<fmt:message key="ACTION_exportAuditResult"/></a>--%>
            <%--</c:if>--%>
        <%--</security:authorize>--%>

        <!-- dealer link -->

        <security:authorize ifAnyGranted="ROLE_DEALER_USER">
            <c:if test="${loanRequest.phase != 'draft' and loanRequest.phase != 'dealer_submit' and loanRequest.phase != 'bank_credit_audit'}">
                <a class="btn btn-default margin" href="/copyRequest?reqId=${loanRequest.id}" title="<fmt:message
                            key="loanRequest.copyHint"/>"><fmt:message
                        key="loanRequestList.copy"/></a>
            </c:if>
            <c:if test="${loanRequest.phase!='credit_denied' and loanRequest.phase!='loan_denied' and loanRequest.phase!='request_denied'}">
            <c:if test="${loanRequest.id != null}">
                <c:if test="${phaseInfo.canSave == true}">
                    <button type="submit" class="btn btn-primary margin" id="saveAndUpload" name="saveAndUpload">
                        <i class="icon-ok icon-white"></i> <fmt:message key="loanRequestDetail.action.uploadFile"/>
                    </button>
                </c:if>
                <c:if test="${phaseInfo.canSave != true}">
                    <a class="btn btn-default margin" href="/attachments?reqId=${loanRequest.id}" ><fmt:message
                            key="loanRequestDetail.action.uploadFile"/></a>
                </c:if>
            </c:if>
            <c:if test="${loanRequest.phase != 'draft' and loanRequest.phase != 'bank_credit_audit'}">
                <a class="btn btn-success" href='/sendMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.send"></fmt:message></a>
            </c:if>
            </c:if>
        </security:authorize>
        <!-- upload -->
        <security:authorize ifNotGranted="ROLE_DEALER_ADMIN,ROLE_DEALER_USER">
            <%--<security:authorize ifAnyGranted="ROLE_Bank_CREDIT">
                <c:if test="${loanRequest.phase == 'bank_credit_audit'}">
                    <c:set var="uploadEnable" scope="page" value="true" />
                </c:if>
            </security:authorize>
            <security:authorize ifAnyGranted="ROLE_CA">
                <c:if test="${loanRequest.phase == 'ca_audit'}">
                    <c:set var="uploadEnable" scope="page" value="true" />
                </c:if>
            </security:authorize>
            <security:authorize ifAnyGranted="ROLE_Bank_RECEIVER">
                <c:if test="${loanRequest.phase == 'pending_loan_material' or loanRequest.phase == 'bank_loan_audit'}">
                    <c:set var="uploadEnable" scope="page" value="true" />
                </c:if>
                <a class="btn btn-success" href='/receiveMaterials?reqId=${loanRequest.id}'><fmt:message key="button.material.receive"></fmt:message></a>
            </security:authorize>
            <security:authorize ifAnyGranted="ROLE_Insurance">
                <c:if test="${loanRequest.phase == 'qualification_audit' or loanRequest.phase == 'insurance_upload_policy'}">
                    <c:set var="uploadEnable" scope="page" value="true" />
                </c:if>
            </security:authorize>--%>

            <security:authorize ifAnyGranted="ROLE_M_RECEIVE">
                <a class="btn btn-success" href='/receiveMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.receive"></fmt:message></a>
                <c:choose>
                    <c:when test="${loanRequest.materialAudit.loanComplete and loanRequest.materialAudit.materialStatus=='Receiving' and loanRequest.status=='loan_audit'}">
                        <a class="btn btn-success" href='/submitMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.submit"></fmt:message></a>
                    </c:when>
                    <c:otherwise>
                        <a class="btn btn-success" href='/submitMaterials?maId=${loanRequest.materialAudit.id}' disabled="true" ><fmt:message key="button.material.submit"></fmt:message></a>
                    </c:otherwise>
                </c:choose>
            </security:authorize>
            <security:authorize ifAnyGranted="ROLE_M_AUDIT">
                <a class="btn btn-success" href='/auditMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.startaudit"></fmt:message></a>
                <c:choose>
                    <c:when test="${loanRequest.materialAudit.materialStatus=='Auditing' and loanRequest.phase=='pending_loan_material'}">
                        <a class="btn btn-success" href='/submitMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.submit"></fmt:message></a>
                    </c:when>
                    <c:otherwise>
                        <a class="btn btn-success" href='/submitMaterials?maId=${loanRequest.materialAudit.id}' disabled="true" ><fmt:message key="button.material.submit"></fmt:message></a>
                    </c:otherwise>
                </c:choose>
            </security:authorize>
            <security:authorize ifAnyGranted="ROLE_M_PRINT">
                <a class="btn btn-success" href="/attachments?reqId=${loanRequest.id}"><fmt:message key="button.material.startprint"/></a>
                <a class="btn btn-success" href='/submitMaterials?maId=${loanRequest.materialAudit.id}'><fmt:message key="button.material.print"></fmt:message></a>
            </security:authorize>

            <security:authorize ifNotGranted="ROLE_M_PRINT">
            <c:if test="${attachmentInfo == null and param.mode!='edit'}">
                <a class="btn btn-default margin" href="/attachments?reqId=${loanRequest.id}"><fmt:message key="loanRequestDetail.action.seeFile"/></a>
            </c:if>
            </security:authorize>

            <c:if test="${attachmentInfo != null}">
                <a class="btn btn-default margin" href="/attachments?reqId=${loanRequest.id}" ><fmt:message
                        key="loanRequestDetail.action.uploadFile"/></a>
            </c:if>
        </security:authorize>

        <security:authorize ifNotGranted="ROLE_DEALER_USER,ROLE_DEALER_ADMIN,ROLE_M_PRINT,ROLE_M_AUDIT,ROLE_M_RECEIVE">
            <c:if test="${loanRequest.status != 'bank_credit_audit' and param.mode!='edit'}">
                <a class="btn btn-default margin" id="open-creditInfo"><fmt:message key="loanRequest.credit.info"/></a>
            </c:if>
        </security:authorize>

    </div>

</div>

<%@include file="/WEB-INF/pages/loanrequestCreditInfo.jsp"%>